// 入口文件
import Vue from 'vue'
// 1.1导入路由的包
import VueRouter from 'vue-router'
// 1.2 安装路由
Vue.use(VueRouter)
// 1.3 导入自己的 router.js 路由模块
import router from './router.js'
// 导入vuex
import Vuex from "vuex"
// 安装vuex
Vue.use(Vuex)
var car = JSON.parse(localStorage.getItem('car') || '[]')
var store = new Vuex.Store({
    state: {
        car: car
    },
    mutations: {
        addToCar(state, goodsinfo) {
            // 添加到购物车
            var flag = false
            state.car.some(item => {
                if (item.id === goodsinfo.id) {
                    item.count += parseInt(goodsinfo.count)
                    flag = true
                    return true
                }
            })

            if (flag === false) { // !flag 理解不到位 
                state.car.push(goodsinfo)
            }

            localStorage.setItem('car',JSON.stringify(state.car))
        }
    },
    getters: {
        getAllCount(state) {
            var middle = 0;
            state.car.forEach(item => {
                middle += item.count
            })
            return middle
        }
    }
})



// 导入时间 mement 插件
import moment from 'moment'

// 定义全局过滤器
Vue.filter('dataFormat', function (dataStr, pattern = "YYYY-MM-DD HH:mm:ss") {
    return moment(dataStr).format(pattern)
})

// 导入 mint-ui 中的组件
import { Header, Swipe, SwipeItem, Lazyload, Button, Spinner, Switch } from "mint-ui"
Vue.component(Header.name, Header)
Vue.component(Swipe.name, Swipe)
Vue.component(SwipeItem.name, SwipeItem)
Vue.use(Lazyload)
Vue.component(Button.name, Button)
Vue.component(Spinner.name, Spinner)
Vue.component(Switch.name, Switch);

//导入vue-resource
import VueResource from "vue-resource"
// 安装vue-resource
Vue.use(VueResource)
// 设置请求的根路径
Vue.http.options.root = "https://api.douban.com/"

// 导入 mui 的样式 
import './lib/mui/css/mui.min.css'
import './lib/mui/css/icons-extra.css'

// 导入app根文件
import app from "./App.vue"

var vm = new Vue({
    el: "#app",
    render: c => c(app),
    router,
    store
})

console.log("ok");
